<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 危险源管理
 * @package app\Hazard
 * @author LJQ<245369209@qq.com>
 */
class Hazard extends Api

{
	protected $wfType = 6;

    /**
   *  危险源列表
   * @param string  string equipeId 企业id,如果有只显示企业自己的信息.
   * @return Json
   */

	public function hazardList($scope = null){
            $where = [] ;
   
            $where['sourceType'] = 1 ;

             if($scope){
      
               $where['enterpriseId'] = $scope;
             }
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 危险源列表
			$info =collection(model('Hazard')->with('enterprise,staff,sensors')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			// 危险源列表
			$info =collection(model('Hazard')->with('enterprise,staff,sensors')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('Hazard')->where($where)->count('id'); 
		
    		if ($count == 0) {
         			return json(['code'=>1,'msg'=>'没有找到危险源','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'危险源信息','count'=>$count,'data'=>$info]);
       		}
         return json(['code'=>0,'msg'=>'数据异常']);
   	 	 
	}
   	  
   	      //添加危险源
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
		 $model = model('Basic/Hazard');

		 if(($data['sensorIds'])){
			 $sensor=$data['sensorIds'];
		 }

		 $result = $model->allowField(true)->save($data);

		 $id=$model->id;

          //配置传感器
		  if($sensor){
			  if(model('Sensor')->where('dangerSourceId',$id)->select()){
				  model('Sensor')->where('dangerSourceId',$id)->update(['dangerSourceId'=>null]);//清空原来配置
         	  }
     	  model('Sensor')->where('id','in',$sensor)->update(['dangerSourceId'=>$id]);
	  }
		 if (false != $result) {
            setlog('Hazardadd','Hazard',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'危险源添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'危险源添加失败,请检查']);
          }
      }

   	

   /**
   * 危险源修改
   * @param  string HazardId 危险源id,
   * @return Json
   */
   	public function edit($data,$hazardId){
        
          if (empty($hazardId)) {
              return json(['code'=>0,'msg'=>'危险源不存在,请检查']);
          }


          //配置传感器
		if(!empty($data['sensorIds'][0])){

			model('Sensor')->where('dangerSourceId',$data['id'])->update(['dangerSourceId'=>null]);//清空原来配置
			if(model('Sensor')->where('id','in',$data['sensorIds'])->update(['dangerSourceId'=>$data['id']])){
				unset($data['sensorIds']);
			}else{
				return json(['code'=>0,'msg'=>'配置传感器失败']);
			}
		}
        $model = model('Basic/Hazard');
		if ( $model->allowField(true)->save($data,['id'=>$hazardId])) {
            setlog('Hazardupdate','Hazard',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'危险源信息修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'危险源信息修改失败,请检查']);
            }
    }
    /**
   *  删除危险源
   * @param string HazardId 危险源id,
   * @param Json
   */
     
    public function del($hazardId) {
      	$hazardId = input('hazardId',0,'intval');
        $model = model('Basic/Hazard');
      	if (empty ( $hazardId )||empty(($model->where(array('id'=>$hazardId))->find()))) {
           	return  json(['code'=>0,'msg'=>'危险源id不存在,请检查']);
        }
        if($model->destroy(['id'=>$hazardId]) !== FALSE){
          setlog('Hazarddel','Hazard',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'危险源删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'危险源删除失败,请检查']);
       }
    }

     
   /**
   *   查询危险源
   * @param keyword危险源名称
   * @return Json
   */
   	public function search($keyword=null,$enterprise=null){
        $where = [] ;
   
        $where['sourceType'] = 1 ;
		if($keyword){
			
			$where['name'] = ['like','%'.$keyword.'%'];
		}

        if ($enterprise) {
            $where['enterpriseId'] = $enterprise;
        }
   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//危险源列表
			$info =collection(model('Hazard')->with('enterprise,staff')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//危险源列表
			$info =collection(model('Hazard')->with('enterprise,staff')->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('Hazard')->where($where)->count('id'); 
		 
    	if (empty($info) && $count == 0) {
            return json(['code'=>1,'msg'=>'没有找到危险源','count'=>$count,'data'=>$info]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'危险源','count'=>$count,'data'=>$info]);
       	}
      }


	/**
	 * 获取危险源位置
	 * @param string $enterpriseId
	 * @return Json
	 */
	public function getHazardPosition($hazardId){
		$position = model('hazard')->where('id',$hazardId)->value('latitudeLongitude');
		if($position){
			return json(['code'=>1,'msg'=>'','data'=>$position]);
		}else{
			return json(['code'=>0,'msg'=>'没有对应位置']);
		}
	}
      
      /**
       * 申请审核
       * @param string $hazardId
       */
      public function audit($hazardId = null){
      	$workflow = model('Workflow')->where('wfType',$this->wfType)->find();
      	if(empty($workflow)||empty($workflow->steps)){
      		return json(['code'=>0,'msg'=>'无对应审核流程','count'=>'','data'=>'']);
      	}else{
      		$hazard = model('Hazard')->get($hazardId);
	      	if($hazard->isUpdate(true)->save(['checkStatus'=>2]) !== false){
	      		$data['title'] = $hazard['name'];
	      		$data['wfFlowInfoId'] = $workflow['id'];
	      		$data['wfType'] = $this->wfType;
	      		$data['busiId'] = $hazardId;
	      		$data['enterpriseId'] = $hazard['enterpriseId'];
	      		$data['applyPerson'] = session('user_auth.id');
	      		if(model('Wfitem')->save($data)){
	      			return json(['code'=>1,'msg'=>'申请成功','count'=>'','data'=>'']);
	      		}else{
	      			return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);	
	      		}
	      	}
      	}
      	return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);	
      	
      }


}